﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics.Contracts;
using System.Windows;
using ShredTheLog.Common.MVVM;
using ShredTheLog.Api;
using System.ComponentModel.Composition;

namespace ShredTheLog.ViewModels
{
    public sealed class InputViewModel : ViewModelBase
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        private Func<string> _prompt;
        private Func<string> _title;
        private Action<string> _okAction;
        public Action CloseWindow { get; set; }

        public InputViewModel(Func<string> prompt, string defaultValue, Action<string> okAction)
        {
            Contract.Requires(prompt != null);
            Contract.Requires(okAction != null);

            _prompt = prompt;
            _title = () => ShredServices.GetLocalizedString("s_AppTitle");
            InitialValue = defaultValue;
            _okAction = okAction;
        }

        public InputViewModel(Func<string> prompt, Func<string> title, string defaultValue, Action<string> okAction)
        {
            Contract.Requires(prompt != null);
            Contract.Requires(title != null);
            Contract.Requires(okAction != null);

            _prompt = prompt;
            _title = title;
            InitialValue = defaultValue;
            _okAction = okAction;
        }

        public string WindowTitle { get { return _title(); } }

        public string Prompt { get { return _prompt(); } }

        public string InitialValue { get; private set; }

        public bool IsWindowClosed
        {
            get { return _isWindowClosed; }
            set { _isWindowClosed = value; OnPropertyChanged(() => IsWindowClosed); }
        }
        private bool _isWindowClosed;

        private ICommand _processInputCommand;
        public ICommand ProcessInputCommand
        {
            get
            {
                if (_processInputCommand == null)
                {
                    _processInputCommand = new DelegateCommand<string>(value => { _okAction(value); IsWindowClosed = true; });
                }
                return _processInputCommand;
            }
        }

        private ICommand _closeCommand;
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new DelegateCommand(() => { IsWindowClosed = true; });
                }
                return _closeCommand;
            }
        }
    }
}
